package com.qdxwx.web.controller.user;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.data.service.user.UserDownloadService;
import com.qdxwx.models.user.User;
import com.qdxwx.models.user.UserDownload;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 个人中心我的下载记录  Controller
 * @author developer
 */
@Controller
@RequestMapping("/user/download")
public class UserDownloadController extends BaseController {

	@Autowired
	private UserDownloadService userDownloadService;

	/**
	 * 下载记录
	 */
	@RequestMapping("/list.html")
	public String study(HttpServletRequest request, ModelMap mmap) {
		User user = LoginUser.getUser(request);
		if (user == null) {
			return "redirect:/auth/login.html";
		}
		int currentPage = TKConvert.toInteger(request.getParameter("page"));
		PageRequest page = new PageRequest();
		page.setCurrentPage(currentPage);
		page.setPageSize(8);
		UserDownload query = new UserDownload();
		query.setUserId(user.getUserId());
		List<UserDownload> userDownloads = userDownloadService.getList(query, page);
		int total = userDownloadService.count(query);
		mmap.put("totalPages", page.getTotalPages(total));
		mmap.put("page", page);
		mmap.put("userDownloads", userDownloads);
		if(TKRequest.isMobileBrowse(request)){
			return getViewPathH5("user/uc-download");
		}
		return getViewPath("ucenter/uc-download");
	}

}